/**
 * 
 */
package de.oszimtcc.timetablecore;

import java.io.Serializable;
import java.util.Date;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import android.util.Log;
import de.oszimtcc.framework.Logger;
import de.oszimtcc.timetable.TimetableApplication;

/**
 * @author Kooki
 * Class to represent one school lesson
 *
 */
public class Block implements Serializable
{
	public enum BlockType
	{
		Standard,
		Changed,
		Cancellation,
		Free;
	}
	
	private String subject;
	private String teacher;
	private String room;
	private String startTime;
	private String endTime;
	private int blockNumber;
	private BlockType blockType = BlockType.Standard;
	private Boolean hasNewChanges = false;
	
	/**
	 * Constructor to create a new Block
	 */
	
	public Block(Element pBlockToCreate)
	{
		this.SetBlockType(pBlockToCreate);
		this.SetSubject(pBlockToCreate);
		this.SetTeacher(pBlockToCreate);
		this.SetRoom(pBlockToCreate);
		this.SetBlockNumber(pBlockToCreate);
		this.SetStartTime();
		this.SetEndTime();
		
		if(this.hasNewChanges && this.blockType == BlockType.Standard)
			this.blockType = BlockType.Changed;
	}
	
	/**
	 * @return the subject
	 */
	public String GetSubject()
	{
		return subject;
	}

	
	/**
	 * @param subject the subject to set
	 */
	public void SetSubject(Element pNode)
	{
		String _subject = GetValueByTag(pNode, "subject");
		Boolean _change = GetAttributeByTag(pNode, "subject", "change").equalsIgnoreCase("true") ? true : false;
		this.subject = _change ? "c_"+_subject : "n_"+_subject;
		if(_change)
			this.hasNewChanges = true;
	}

	
	/**
	 * @return the teacher
	 */
	public String GetTeacher()
	{
		return teacher;
	}

	
	/**
	 * @param teacher the teacher to set
	 */
	public void SetTeacher(Element pNode)
	{
		String _teacher = GetValueByTag(pNode, "teacher");
		Boolean _change = GetAttributeByTag(pNode, "teacher", "change").equalsIgnoreCase("true") ? true : false;
		this.teacher = _change ? "c_"+_teacher : "n_"+_teacher;
		if(_change)
			this.hasNewChanges = true;
	}

	
	/**
	 * @return the room
	 */
	public String GetRoom()
	{
		return room;
	}

	
	/**
	 * @param room the room to set
	 */
	public void SetRoom(Element pNode)
	{
		String _room = GetValueByTag(pNode, "room");
		Boolean _change = GetAttributeByTag(pNode, "room", "change").equalsIgnoreCase("true") ? true : false;
		this.room = _change ? "c_"+_room : "n_"+_room;
		if(_change)
			this.hasNewChanges = true;
	}

	
	/**
	 * @return the startTime
	 */
	public String GetStartTime()
	{
		return startTime;
	}

	
	/**
	 * @param startTime the startTime to set
	 */
	public void SetStartTime()
	{
		this.startTime = Block.GetStartTimeFromBlock(this.GetBlockNumber());
	}

	
	/**
	 * @return the endTime
	 */
	public String GetEndTime()
	{
		return endTime;
	}

	
	/**
	 * @param endTime the endTime to set
	 */
	public void SetEndTime()
	{
		this.endTime = Block.GetEndTimeFromBlock(this.GetBlockNumber());
	}

	
	/**
	 * @return the blockNumber
	 */
	public int GetBlockNumber()
	{
		return blockNumber;
	}

	
	/**
	 * @param blockNumber the blockNumber to set
	 */
	public void SetBlockNumber(Element pNode)
	{
		String _block = GetAttributeByTag((Element)pNode, "block", "nr");
		this.blockNumber = Integer.parseInt(_block);
	}

	
	/**
	 * @return the hasChanges
	 */
	public Boolean GetHasNewChanges()
	{
		return hasNewChanges;
	}

	
	/**
	 * @param hasChanges the hasChanges to set
	 */
	public void SetHasNewChanges(Boolean pHasNewChanges)
	{
		this.hasNewChanges = pHasNewChanges;
		this.blockType = this.GetBlockType() == BlockType.Standard ?  BlockType.Changed : this.GetBlockType();
	}

	
	
	/**
	 * @return the blockType
	 */
	public BlockType GetBlockType()
	{
		return blockType;
	}

	
	/**
	 * @param blockType the blockType to set
	 */
	public void SetBlockType(Element pNode)
	{
		if(GetAttributeByTag(pNode, "block", "lesson").equalsIgnoreCase("false"))
		{	
			this.blockType = BlockType.Free;
			return;
		}
		if(GetAttributeByTag(pNode, "block", "canceled").equalsIgnoreCase("true"))
		{	
			this.blockType = BlockType.Cancellation;
			return;
		}
	}

	public static String GetStartTimeFromBlock(int pBlockNumber)
	{
		switch(pBlockNumber)
		{
			case 1:
				return "08:00";
			case 2:
				return "09:45";
			case 3:
				return "11:35";
			case 4:
				return "13:30";
			case 5:
				return "15:15";			
			case 6:
				return "17:00";
			default:
				return "Unknown StartTime";
		}
	}
	
	public static String GetEndTimeFromBlock(int pBlockNumber)
	{
		switch(pBlockNumber)
		{
			case 1:
				return "09:30";
			case 2:
				return "11:15";
			case 3:
				return "13:05";
			case 4:
				return "15:00";
			case 5:
				return "16:45";			
			case 6:
				return "18:30";
			default:
				return "Unknown EndTime";
		}
	}
	
    private String GetValueByTag(Element pElement, String pTag) 
    {
		Element toReturn = (Element)pElement.getElementsByTagName(pTag).item(0);
		Logger.Debug("Get  value " + toReturn.getTextContent().trim() + " from tag " + pTag);
		return toReturn.getTextContent().trim();    	
    }
    
    private String GetAttributeByTag(Element pElement, String pTag, String pAttributeTag) 
    {
    	String toReturn = "";
    	Element tmp = pElement.getNodeName().trim().equals("block") ? pElement : (Element)pElement.getElementsByTagName(pTag).item(0);
    	toReturn = tmp.getAttribute(pAttributeTag).trim();
		Logger.Debug("Get " + tmp.getAttribute(pAttributeTag).trim() + " from attribute " + pAttributeTag + " on tag " + pTag);
		return toReturn;    	
    }

}
